#include"common_headers.h"

int updatefile31(char *file_path, char* data)
{
	//DECLARATIONS:
    FILE *fptr2=NULL,*fptr=NULL;
    struct Block *block= NULL;
    struct FileDescriptor *fd_Desc1=NULL;
    struct BST_Node* temp;
    char output_file_path[300], filenameFromfilepath[50], *result, name[50];
    char delims[]="/";
    int m,found=0,pos,retval=1,length, collect=0, is_mounted, is_source_file_exist, is_path_exist_in_nary, is_correct_file_format;
    
    
    //CHECK IF VFS MOUNTED
    is_mounted = validateMountState();
    if(is_mounted)
    {
    	//CHECK IF SOURCE FILE EXISTS (EXISTS IN HARD DISK)
    	is_source_file_exist = validateFileExistance(data);
    	if(is_source_file_exist)
    	{
    		//CHECK IF DESTINATION PATH EXIST (IN VFS)
    		is_path_exist_in_nary = search_nary(file_path);
    		if(is_path_exist_in_nary)
    		{    		
    			strcpy(output_file_path,data);
				
				//EXTRACTING FILENAME FROM OUTPUT FILE PATH					
				result = strtok(output_file_path, delims);
				while(result!=NULL)
				{
					strcpy(filenameFromfilepath, result);
					result=strtok(NULL, delims);
				}
				strcpy(name,filenameFromfilepath);	//FILENAME EXTRACTED IN "name" VARIABLE
			
    			//CHECK IF CORRECT FILE FORMAT
    			is_correct_file_format = validateTextFile(name);
    			if(is_correct_file_format)
    			{
    				//CHECK IF FILE TOO LARGE
					fptr2=fopen(data,"r");
					fseek(fptr2,0,SEEK_END);
					length=ftell(fptr2);
					
					if(length <= 1024)
					{					
						fseek(fptr2,0,SEEK_SET);
						block= (struct Block *) malloc(sizeof(struct Block));
						fread(block->block,blocksize,1,fptr2);
						fclose(fptr2);
						
						//TRAVERSE THROUGH THE FREE LIST UNTIL MAX FD's AND INSERT THE FILE DETAILS
						for(m=0; m<mainHeader.maximumFileDescriptor; m++)
						{
							fd_Desc1=&fileDescriptors[m];
							if(freelist[m]==1 && strcmp(fd_Desc1->locationpath,file_path)==0 && strcmp(fd_Desc1->filetype,"file")==0)
							{
								found=1;
								pos=m;
								break;
							}
						}

						if(found)
						{
							fptr=fopen(mainHeader.filelabel,"rb+");
							if( fptr != NULL )
							{
								fseek(fptr,(sizeof(struct MainHeader)+(mainHeader.maximumFileDescriptor*sizeof(char))+(mainHeader.maximumFileDescriptor*sizeof(struct FileDescriptor))+(pos*sizeof(struct Block))),SEEK_SET);
								fwrite(block,sizeof(struct Block),1,fptr);
								fclose(fptr);
								
								collect=1;	
								printf("updatefile_SUCCESS\n");							
							}
							else
							{
								printf("\nupdatefile:specified vfs file does not exist which can never happen");
							}
						}
						else
						{
							//MAY BE NOT NEEDED (COZ V R ALREADY KEEPING A CHECK TO SEE IF SOURCE FILE EXISTS IN THE VFS)
							printf("\nNO ENTRY IN FREE LIST , FILE NOT FOUND");
						}
						
					}
					else
					{
						//FILE_TOO_LARGE
						printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_04);
					}
    			}
    			else
    			{
    				//INCORRECT_FILE_FORMAT
    				printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_03);	
    			}    			
    		}
    		else
    		{
    			//DEST_FILE_PATH_NOT_FOUND
    			printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_02);	
    		}
    	}
    	else
    	{
    		//SOURCE_FILE_PATH_NOT_FOUND
    		printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_01);	
    	}
    }
    else
    {
    	//VFS NOT MOUNTED
    	printf("updatefile_FAILURE %s\n",ERR_VFS_UPDATEFILE_06);
    }
    
    return collect;
    
   
    
}
